package com.yunc.user.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.user.sys.entity.SysUserRole;
import com.yunc.user.sys.mapper.SysUserRoleMapper;
import com.yunc.user.sys.service.ISysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统角色
 *
 * @author congcong tao
 * @date 2018/5/14 16:17
 */
@Service("sysUserRoleService")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    private final SysUserRoleMapper sysUserRoleMapper;

    @Override
    public boolean insertList(String userId, String roleIds) {
        if (StringUtils.isNotEmpty(roleIds)) {
            String[] roleIdArr = roleIds.split(",");
            if (roleIdArr != null && roleIdArr.length > 0) {
                List<SysUserRole> sysUserRoleList = new ArrayList<>();
                for (String roleId : roleIdArr) {
                    SysUserRole userRole = new SysUserRole();
                    userRole.setUserId(userId);
                    userRole.setRoleId(roleId);
                    if (!sysUserRoleList.contains(userRole)) {
                        sysUserRoleList.add(userRole);
                    }
                }
                boolean flag = this.saveBatch(sysUserRoleList);
                if (!flag) {
                    return false;
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public List<String> listRoleList(String userId) {
        return sysUserRoleMapper.listRoleList(userId);
    }
}
